<?php
namespace app\service;
use think\facade\Db;
class lockService{
    public function lockList($com){
        return Db::name('lock')->where($com)->order('id asc')->paginate(100);
    }
    public function communityName($comid){
        return Db::name('community')->where('id',$comid)->find();
    }
    public function communityList($whe){
        return Db::name('community')->order('id asc')->where($whe)->select();
    }
    public function lockAdd($data){
        return Db::name('lock')->insert($data);
    }
    public function lockId($serial){
        return Db::name('lock')->where('serial',$serial)->find();
    }
    public function lockEdit($data){
        return Db::name('lock')->update($data);
    }
    public function lockDel($serial){
        return Db::name('lock')->where('serial',$serial)->delete();
    }
    public function communityId($community){
        return Db::name('community')->where('id',$community)->find();
    }
    public function propertyId($propertyid){
        return Db::name('property')->where('id',$propertyid)->find();
    }
    public function alipayAdd($alipay,$id){
        return Db::name('room')->where('id',$id)->update(['alipay' => $alipay]);
    }
    public function roomstatus($community,$building,$unit,$room){
        return Db::name('room')->where('community',$community)->where('building',$building)->where('unit',$unit)->where('room',$room)->update(['status' => '1']);
    }
    public function roomIdInfo($community,$building,$unit,$room){
        return Db::name('room')->where('community',$community)->where('building',$building)->where('unit',$unit)->where('room',$room)->find();
    }
    public function roomLock($id,$lock){
        return Db::name('room')->where('id',$id)->update(['lock' => $lock]);
    }
    public function roomOpen($community,$building,$unit,$room,$locksnid){
        return Db::name('room')->where('community',$community)->where('building',$building)->where('unit',$unit)->where('room',$room)->where('lock','in',$locksnid)->find();
    }
    //根据小区名称查找楼栋信息
    public function groupList($community){
        return Db::name('room')->where('community',$community)->column('group');
    }
    //根据小区名称查找楼栋信息
    public function buildingList($community,$group){
        return Db::name('room')->where('community',$community)->where('group',$group)->order('id asc')->column('building');
    }
    //根据楼栋查询单元
    public function unitList($community,$group,$building){
        return Db::name('room')->where('community',$community)->where('group',$group)->where('building',$building)->order('id asc')->column('unit');
    }
    //根据单元查询房屋
    public function roomList($community,$group,$building,$unit){
        return Db::name('room')->where('community',$community)->where('group',$group)->where('building',$building)->where('unit',$unit)->order('id asc')->column('room');
    }
    //判断当前位置是否已经添加门锁
    public function lockPosition($community,$position){
        return Db::name('lock')->where('community',$community)->where('position',$position)->find();
    }


    public function door($serial,$operation){
        $appid = 'wmj_T8lCr7t6fTc';//wmj_C5cpRWjAYbK
        $appsecret = 'rpwjSBEgdoBkod7YAYzlMpg6mH01DafX';//UewpNee3t4r0LY9J9gcRzEol0uXQid2W
        $aeskey = 'GaqpopNUMzgD8UsIceQZoP1vlbZLPZXY'; //AES加密密钥，需要和开放平台保持一致，否则会报错，如未启用加密，请留空LEKQaYeHDrJLbH5VpOf8ov5nF5ObnvA8
        if(empty($serial)){
            echo '门锁错误';
            exit();
        }
        $locksn = $this->aesEncrypt($serial, $aeskey);//锁的序列号，这个序列号贴在每个模块的标签上。
        if(!empty($operation)){
            if($operation == 1){
                $result = $this->httpPost('https://www.wmj.com.cn/api/postlock.html?appid='.$appid.'&appsecret='.$appsecret, $locksn);//DEMO -- 提交模块到系统注册
            }elseif($operation == 2){
                $result = $this->httpPost('https://www.wmj.com.cn/api/lockstate.html?appid='.$appid.'&appsecret='.$appsecret, $locksn);//DEMO -- 查询模块在线状态
            }elseif($operation == 3){
                $result = $this->httpPost('https://www.wmj.com.cn/api/dellock.html?appid='.$appid.'&appsecret='.$appsecret, $locksn);//DEMO -- 删除模块接口
            }elseif($operation == 4){
                $result = $this->httpPost('https://www.wmj.com.cn/api/openlock.html?appid='.$appid.'&appsecret='.$appsecret, $locksn);//DEMO -- 开门接口
            }else{
                echo '错误操作';
                exit();
            }
        }
        $result = trim($result, "\xEF\xBB\xBF"); //去除BOM头
        return(json_decode($result, true));
    }
    /*
    *DEMO -- httpPost函数
    */
    function httpPost($url,$str){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $str);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($str))
        );
        $res = curl_exec ($curl);
        curl_close($curl);
        return $res;
    }

    /*
     * AES加密 -- 加密采用128位CBC模式加密
     */
    function aesEncrypt ($value, $key) {
        $padSize = 16 - (strlen($value) % 16);
        $value   = $value . str_repeat(chr($padSize), $padSize) ;
        $output  = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_CBC, str_repeat(chr(0), 16));

        return base64_encode($output);
    }
}